<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\admin\model\Member as MemberModel;
use app\admin\model\AuthGroup as GroupModel;

class Member extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new MemberModel();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createItem($data);
            if($res["status"]){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], 'index');
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg']);
            }
        }else{
            $GroupModel = new GroupModel;
            $groups = $GroupModel->selectOptions();
            $this->assign('groups', $groups);
            $this->assign('member', ['group_id' => 0, 'status' => 1]);
            return $this->fetch();
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id = 0)
    {
        $member = $this->Model->findOne($id);
        if(empty($member)){
            Session::flash('danger', '未找到该管理员信息');
            $this->redirect('index');
        }
        $this->assign('member', $member);
        return $this->fetch();
    }

    /**
    * 保存更新的资源
    *
    * @param  \think\Request  $request
    * @param  int  $id
    * @return \think\Response
    */
    public function update($id = 0)
    {
        $id = intval($id);
        $member = $this->Model->where('id', $id)->find();
        if(empty($member)){
            $this->redirect('index');
        }

        if($this->request->isPost()){
            $data = $this->getPostData();
            $data['id'] = $id;
            $res = $this->Model->updateItem($data);
            if($res['status']){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], 'index');
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg']);
            }
        }else{
            $GroupModel = new GroupModel;
            $groups = $GroupModel->selectOptions();
            $this->assign('groups', $groups);
            $this->assign('member', $member);
            return $this->fetch();
        }
    }

    private function getPostData()
    {
        $fields = [
            ['name' => 'name', 'type' => 'string', 'default' => ''],
            ['name' => 'nickname', 'type' => 'string', 'default' => ''],
            // 使用switch开关，所以默认值必须是false，因为switch为关闭状态时返回的值里没有该字段
            ['name' => 'status', 'type' => 'boolean', 'default' => false],
            ['name' => 'group_id', 'type' => 'integer', 'default' => 0],
            ['name' => 'password', 'type' => 'string', 'default' => '', 'is_null' => true],
            ['name' => 'confirm_password', 'type' => 'string', 'default' => '', 'is_null' => true],
            ['name' => 'email', 'type' => 'string', 'default' => '', 'is_null' => true],
            ['name' => 'avatar', 'type' => 'string', 'default' => '', 'is_null' => true],
            ['name' => 'gender', 'type' => 'string', 'default' => 'secret', 'is_null' => true],
            ['name' => 'mobile', 'type' => 'string', 'default' => '', 'is_null' => true],
        ];

        return $this->parsePostParam($fields, []);
    }
}
